﻿using System;
using System.Linq;
using System.Web.Mvc;
using BLL;
using Model;
using Web.Helper;

namespace Web.Areas.Manager.Controllers
{
    [UserAuthorize]
    public class EfficiencyLogAllController : Controller
    {
        EfficiencyLog_BLL _EfficiencyLog_BLL = new EfficiencyLog_BLL();
        EfficiencyLogCycle_BLL _EfficiencyLogCycle_BLL = new EfficiencyLogCycle_BLL();
        Sys_Users_BLL _Sys_Users_BLL = new Sys_Users_BLL();

        public virtual ActionResult Index(int? pageIndex, string keyword, DateTime? date)
        {
            var model = _EfficiencyLog_BLL.Get();
            if (!string.IsNullOrEmpty(keyword))
            {
                model = model.Where(a => a.Title.Contains(keyword) || a.Sys_Users.UserName.Contains(keyword));
            }
            else if (date != null)
            {
                model = model.Where(a => a.AddDate.Year == date.Value.Year && a.AddDate.Month == date.Value.Month && a.AddDate.Day == date.Value.Day);

            }
            return View(model.ToPagedList(pageIndex));
        }


        public virtual ActionResult Details(Guid Id)
        {
            ViewBag.EfficiencyLogCycle = _EfficiencyLogCycle_BLL.Get();

            var item = _EfficiencyLog_BLL.Get(Id);
            return View(item);
        }

        public virtual ActionResult Edit(Guid? Id)
        {
            ViewBag.EfficiencyLogCycle = _EfficiencyLogCycle_BLL.Get();

            var item = _EfficiencyLog_BLL.Get(Id);
            return View(item);
        }

        [HttpPost]
        [ValidateInput(false)]
        public virtual ActionResult Edit(Guid? Id, EfficiencyLog collection)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.EfficiencyLogCycle = _EfficiencyLogCycle_BLL.Get();
                return View(collection);
            }

            collection.UserId = _Sys_Users_BLL.Get(User.Identity.Name).Id;

            _EfficiencyLog_BLL.Save(Id, ref collection);
            return RedirectToAction("Index", MyrouteValues.Get());
        }

        [HttpDelete]
        public virtual ActionResult Delete(Guid Id)
        {
            _EfficiencyLog_BLL.Delete(Id);
            return RedirectToAction("Index", MyrouteValues.Get());
        }
    }
}
